package org.apache.lucene.distributed.index;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.Serializable;
import java.rmi.Remote;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.distributed.index.IndexManagerService.IndexInfo;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;

/**
 * Represents an index on a server.  
 */
public interface IndexService extends Remote {
  public static class IndexVersion implements Serializable {
    public static final long serialVersionUID = 1l;
    public long generation;
  }
  
  /**
   * A changed to the index
   */
  public static interface Operation {
  }
  
  /**
   * An addDocument call 
   *
   */
  public static class Add implements Serializable, Operation {
    public static final long serialVersionUID = 1l;
    public Analyzer analyzer;
    public Document document;
  }
  
  /**
   * An updateDocument call 
   *
   */
  public static class Update implements Serializable, Operation {
    public static final long serialVersionUID = 1l;
    public Document document;
    public Term term;
    public Analyzer analyzer;
  }
  
  /**
   * A deleteDocuments call
   *
   */
  public static class Delete implements Serializable, Operation {
    public static final long serialVersionUID = 1l;
    public Query query;
    public Term term;
  }
  
  public SearchableService reopen() throws Exception;
  public void close() throws Exception;
  public IndexInfo getIndexInfo() throws Exception;
  
  /**
   * Register for notification of reopen being called
   * @param indexListener
   * @throws Exception
   */
  public void registerIndexListener(IndexListener indexListener) throws Exception;
  
  /**
   * Notifies implementation of a new SearchableService on the server
   */
  public static interface IndexListener extends Remote {
    public void newSearchable(SearchableService searchableService) throws Exception;
  }
  
  /**
   * Executes batch of index changing operations (add, update, or delete) 
   * @param operations
   * @throws Exception
   */
  public void execute(Operation[] operations) throws Exception;
  public void addDocument(Document document, Analyzer analyzer) throws Exception;
  public void updateDocument(Term term, Document document, Analyzer analyzer) throws Exception;
  public void deleteDocuments(Term term) throws Exception;
  public void deleteDocuments(Query query) throws Exception;
  
  /**
   * Flushes changes to disk
   */
  public void flush() throws Exception;
  
  /**
   * Flushes changes to disk and reopens the index returning the
   * latest SearchableService
   */
  public SearchableService flushAndReopen() throws Exception;
}
